package com.zg.datapush.web;

import com.zg.common.core.R;
import com.zg.datapush.req.DatasourceCodeReq;
import com.zg.datapush.req.TableStructureReq;
import com.zg.datapush.service.TableStructureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 表结构查询Controller
 * @author zg
 */
@RestController
@RequestMapping("/datapush/tableStructure")
@RequiredArgsConstructor
@Api(tags = "表结构查询")
public class TableStructureController {

    private final TableStructureService tableStructureService;

    @ApiOperation("获取数据库中的所有表")
    @PostMapping("/tables")
    public R<List<String>> getTables(@RequestBody @Valid DatasourceCodeReq req) {
        try {
            List<String> result = tableStructureService.getTables(req.getCode());
            return R.success(result);
        } catch (Exception e) {
            return R.error("获取表列表失败: " + e.getMessage());
        }
    }

    @ApiOperation("获取表的字段信息")
    @PostMapping("/columns")
    public R<List<Map<String, Object>>> getTableColumns(@RequestBody @Valid TableStructureReq req) {
        try {
            List<Map<String, Object>> result = tableStructureService.getTableColumns(req.getDatasourceCode(), req.getTableName());
            return R.success(result);
        } catch (Exception e) {
            return R.error("获取表字段信息失败: " + e.getMessage());
        }
    }
}
